package com.srh.stockmanager.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
//import org.springframework.validation.BindingResult;
//import org.springframework.web.bind.annotation.ModelAttribute;
//import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.srh.stockmanager.enums.TipoPermiso;
import com.srh.stockmanager.form.UserForm;
import com.srh.stockmanager.service.UserService;

//import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 
 * @author Sebastian R Helguera (srhelguera@hotmail.com)
 * @version 01/04/2012
 */
@Controller
public class StartController {

	@Autowired
	private UserService	userService;

	/**
	 * 
	 * @param map
	 * @return String redirect
	 */
	@RequestMapping("/index")
	public String listContacts(Map<String, Object> map) {

		map.put("user", new UserForm());
		map.put("permisos", TipoPermiso.values());
		map.put("userList",	UserForm.userListConvertToForm(userService.listUser()));

		return "user/abmUser";
	}

	/**
	 * 
	 * @param user
	 * @param result
	 * @return String redirect
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addUser(@ModelAttribute("user") UserForm user,
			BindingResult result) {

		userService.addUser(UserForm.userFormConvertToUser(user));

		return "redirect:/index";
	}

	/**
	 * 
	 * @param userId
	 * @return String redirect
	 */
	@RequestMapping("/delete/{userId}")
	public String deleteUser(@PathVariable("userId") Long userId) {

		userService.removeUser(userId);

		return "redirect:/index";
	}
}
